package com.cloud.mall.user.service.impl;

import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.ums.mapper.UmsResourceCategoryMapper;
import com.cloud.mall.ums.model.UmsResourceCategory;
import com.cloud.mall.ums.model.UmsResourceCategoryExample;
import com.cloud.mall.user.input.UmsResourceCategoryInpDTO;
import com.cloud.mall.user.output.UmsResourceCategoryOut;
import com.cloud.mall.usr.service.UmsResourceCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class UmsResourceCategoryServiceImpl implements UmsResourceCategoryService {
    @Autowired
    private UmsResourceCategoryMapper resourceCategoryMapper;

    @Override
    public BaseResponse<List<UmsResourceCategoryOut>> listAll() {
        UmsResourceCategoryExample example = new UmsResourceCategoryExample();
        example.setOrderByClause("sort desc");
        List<UmsResourceCategory> resourceCategoryList= resourceCategoryMapper.selectByExample(example);
        return BaseResponse.setResultSuccess(MallBeanUtils.copyDoToDto(resourceCategoryList, UmsResourceCategoryOut.class));
    }

    @Override
    public BaseResponse create(@RequestBody UmsResourceCategoryInpDTO umsResourceCategoryInpDTO) {
        UmsResourceCategory umsResourceCategory = MallBeanUtils.dtoToDo(umsResourceCategoryInpDTO, UmsResourceCategory.class);
        umsResourceCategory.setCreateTime(new Date());
        int count=resourceCategoryMapper.insert(umsResourceCategory);
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse update(Long id,  @RequestBody UmsResourceCategoryInpDTO umsResourceCategoryInpDTO) {
        UmsResourceCategory umsResourceCategory = MallBeanUtils.dtoToDo(umsResourceCategoryInpDTO, UmsResourceCategory.class);
        umsResourceCategory.setId(id);
        int count=resourceCategoryMapper.updateByPrimaryKeySelective(umsResourceCategory);
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse delete(Long id) {
        int count= resourceCategoryMapper.deleteByPrimaryKey(id);
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }
}
